package com.kotlin.flong.coroutines1

import kotlinx.coroutines.*

/**
 * @Author:liangjl
 * @Date: 2020/7/9-20:17
 * @Eamil:jilongliang@sina.com
 * @Description:
 * 1、ThreadLocal 相关
 * 2、ThreadContextElement.asContextElement在本地线程相关的分析
 * 3、VM options --> -Dkotlinx.coroutines.debug
 */

var threadLocal = ThreadLocal<String>()

fun main() = runBlocking<Unit> {

    threadLocal.set("hello")

    println("pre main ,current thread ${Thread.currentThread()} value = ${threadLocal.get()}")

    var job = launch(Dispatchers.Default + threadLocal.asContextElement(value = "world")) {
        println("launch start ,current thread ${Thread.currentThread()} value = ${threadLocal.get()}")
        //将线程让出来
        yield()
        println("launch start ,current thread ${Thread.currentThread()} value = ${threadLocal.get()}")
    }

    job.join()

    println("post main ,current thread ${Thread.currentThread()} value = ${threadLocal.get()}")

}
